﻿
// Author:					Doan Quoc Tuan
// Created:					2012-6-19
// Last Modified:			2012-6-19
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;

namespace SMSPanasonic.Data
{

    public static class DBSmsRouter
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return Properties.Settings.Default.ConnectionString;

        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {

            return Properties.Settings.Default.ConnectionString;

        }

        /// <summary>
        /// Inserts a row in the SmsRouter table. Returns new integer id.
        /// </summary>
        /// <param name="routerName"> routerName </param>
        /// <param name="routerDescription"> routerDescription </param>
        /// <param name="typeOfObject"> typeOfObject </param>
        /// <param name="routerURL"> routerURL </param>
        /// <param name="routerURL2"> routerURL2 </param>
        /// <param name="userName"> userName </param>
        /// <param name="password"> password </param>
        /// <param name="routerType"> routerType </param>
        /// <param name="publicKey"> publicKey </param>
        /// <param name="publicPrivateKey"> publicPrivateKey </param>
        /// <param name="enabled"> enabled </param>
        /// <param name="numOfThread"> numOfThread </param>
        /// <returns>int</returns>
        public static int Create(
            SqlString routerName,
            SqlString routerDescription,
            SqlString typeOfObject,
            SqlString routerURL,
            SqlString routerURL2,
            SqlString userName,
            SqlString password,
            SqlString routerType,
            SqlString publicKey,
            SqlString publicPrivateKey,
            SqlBoolean enabled,
            SqlInt32 numOfThread)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "SmsRouter_Insert", 12);
            sph.DefineSqlParameter("@RouterName", SqlDbType.NVarChar, 100, ParameterDirection.Input, routerName);
            sph.DefineSqlParameter("@RouterDescription", SqlDbType.NText, ParameterDirection.Input, routerDescription);
            sph.DefineSqlParameter("@TypeOfObject", SqlDbType.VarChar, 200, ParameterDirection.Input, typeOfObject);
            sph.DefineSqlParameter("@RouterURL", SqlDbType.NVarChar, 1000, ParameterDirection.Input, routerURL);
            sph.DefineSqlParameter("@RouterURL2", SqlDbType.NVarChar, 1000, ParameterDirection.Input, routerURL2);
            sph.DefineSqlParameter("@UserName", SqlDbType.NVarChar, 1000, ParameterDirection.Input, userName);
            sph.DefineSqlParameter("@Password", SqlDbType.NVarChar, 1000, ParameterDirection.Input, password);
            sph.DefineSqlParameter("@RouterType", SqlDbType.VarChar, 50, ParameterDirection.Input, routerType);
            sph.DefineSqlParameter("@PublicKey", SqlDbType.VarChar, 2000, ParameterDirection.Input, publicKey);
            sph.DefineSqlParameter("@PublicPrivateKey", SqlDbType.VarChar, 2000, ParameterDirection.Input, publicPrivateKey);
            sph.DefineSqlParameter("@Enabled", SqlDbType.Bit, ParameterDirection.Input, enabled);
            sph.DefineSqlParameter("@NumOfThread", SqlDbType.Int, ParameterDirection.Input, numOfThread);
            int newID = Convert.ToInt32(sph.ExecuteScalar());
            return newID;
        }


        /// <summary>
        /// Updates a row in the SmsRouter table. Returns true if row updated.
        /// </summary>
        /// <param name="routerID"> routerID </param>
        /// <param name="routerName"> routerName </param>
        /// <param name="routerDescription"> routerDescription </param>
        /// <param name="typeOfObject"> typeOfObject </param>
        /// <param name="routerURL"> routerURL </param>
        /// <param name="routerURL2"> routerURL2 </param>
        /// <param name="userName"> userName </param>
        /// <param name="password"> password </param>
        /// <param name="routerType"> routerType </param>
        /// <param name="publicKey"> publicKey </param>
        /// <param name="publicPrivateKey"> publicPrivateKey </param>
        /// <param name="enabled"> enabled </param>
        /// <param name="numOfThread"> numOfThread </param>
        /// <returns>bool</returns>
        public static bool Update(
            SqlInt32 routerID,
            SqlString routerName,
            SqlString routerDescription,
            SqlString typeOfObject,
            SqlString routerURL,
            SqlString routerURL2,
            SqlString userName,
            SqlString password,
            SqlString routerType,
            SqlString publicKey,
            SqlString publicPrivateKey,
            SqlBoolean enabled,
            SqlInt32 numOfThread)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "SmsRouter_Update", 13);
            sph.DefineSqlParameter("@RouterID", SqlDbType.Int, ParameterDirection.Input, routerID);
            sph.DefineSqlParameter("@RouterName", SqlDbType.NVarChar, 100, ParameterDirection.Input, routerName);
            sph.DefineSqlParameter("@RouterDescription", SqlDbType.NText, ParameterDirection.Input, routerDescription);
            sph.DefineSqlParameter("@TypeOfObject", SqlDbType.VarChar, 200, ParameterDirection.Input, typeOfObject);
            sph.DefineSqlParameter("@RouterURL", SqlDbType.NVarChar, 1000, ParameterDirection.Input, routerURL);
            sph.DefineSqlParameter("@RouterURL2", SqlDbType.NVarChar, 1000, ParameterDirection.Input, routerURL2);
            sph.DefineSqlParameter("@UserName", SqlDbType.NVarChar, 1000, ParameterDirection.Input, userName);
            sph.DefineSqlParameter("@Password", SqlDbType.NVarChar, 1000, ParameterDirection.Input, password);
            sph.DefineSqlParameter("@RouterType", SqlDbType.VarChar, 50, ParameterDirection.Input, routerType);
            sph.DefineSqlParameter("@PublicKey", SqlDbType.VarChar, 2000, ParameterDirection.Input, publicKey);
            sph.DefineSqlParameter("@PublicPrivateKey", SqlDbType.VarChar, 2000, ParameterDirection.Input, publicPrivateKey);
            sph.DefineSqlParameter("@Enabled", SqlDbType.Bit, ParameterDirection.Input, enabled);
            sph.DefineSqlParameter("@NumOfThread", SqlDbType.Int, ParameterDirection.Input, numOfThread);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Deletes a row from the SmsRouter table. Returns true if row deleted.
        /// </summary>
        /// <param name="routerID"> routerID </param>
        /// <returns>bool</returns>
        public static bool Delete(
            SqlInt32 routerID)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "SmsRouter_Delete", 1);
            sph.DefineSqlParameter("@RouterID", SqlDbType.Int, ParameterDirection.Input, routerID);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Gets an IDataReader with one row from the SmsRouter table.
        /// </summary>
        /// <param name="routerID"> routerID </param>
        public static IDataReader GetOne(
            SqlInt32 routerID)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "SmsRouter_SelectOne", 1);
            sph.DefineSqlParameter("@RouterID", SqlDbType.Int, ParameterDirection.Input, routerID);
            return sph.ExecuteReader();

        }

        /// <summary>
        /// Gets a count of rows in the SmsRouter table.
        /// </summary>
        public static int GetCount(string sWhereCondition)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "SmsRouter_GetCount", 1);
            sph.DefineSqlParameter("@WhereCondition", SqlDbType.NVarChar, ParameterDirection.Input, sWhereCondition);
            return Convert.ToInt32(sph.ExecuteScalar());

        }

        /// <summary>
        /// Gets an IDataReader with all rows in the SmsRouter table.
        /// </summary>
        public static IDataReader GetAll()
        {

            return SqlHelper.ExecuteReader(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "SmsRouter_SelectAll",
                null);

        }

        /// <summary>
        /// Gets a page of data from the SmsRouter table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
            string whereCondition,
            string orderBy,
            int pageNumber,
            int pageSize,
            out int totalPages)
        {
            totalPages = 1;
            int totalRows
                = GetCount(whereCondition);

            if (pageSize > 0) totalPages = totalRows / pageSize;

            if (totalRows <= pageSize)
            {
                totalPages = 1;
            }
            else
            {
                int remainder;
                Math.DivRem(totalRows, pageSize, out remainder);
                if (remainder > 0)
                {
                    totalPages += 1;
                }
            }

            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "SmsRouter_SelectPage", 4);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            sph.DefineSqlParameter("@WhereCondition", SqlDbType.NVarChar, ParameterDirection.Input, whereCondition);
            sph.DefineSqlParameter("@OrderBy", SqlDbType.NVarChar, ParameterDirection.Input, orderBy);

            return sph.ExecuteReader();

        }

    }

}


